﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using Infrastucture;
using Infrastucture.Navigation;
using NavigationModule.ViewModels;
using TimeSplicerModule.Security;
using TimeSplicerModule.DomainModels;

namespace NavigationModule.Services
{
    public interface ITopNavigationService : IService<TopNavigationViewModel>
    {
        
    }

    public class TopNavigationService : Service, ITopNavigationService
    {
        private ISession _session;

        public TopNavigationService()
            : base()
        {
            _session = this.Container.Resolve<ISession>();
        }
        public TopNavigationService(ISession session)
            : base()
        {
            _session = session;
        }
        public TopNavigationService(IUnityContainer container)
            : base(container)
        {
            _session = container.Resolve<ISession>();
        }


        public TopNavigationViewModel Get()
        {
            // TODO: Need to get session
            //ISession session = this.Container.Resolve<ISession>();
            _session = this.Container.Resolve<ISession>();
            var model = new TopNavigationViewModel();

            if (_session != null && _session.User != null)
            {
                // get access levels for comnpare
                var notAllowed = _session.ApplicationPermissions.FirstOrDefault(x => x.Permission == ApplicationPermission.Not_Allowed);
                var developer = _session.ApplicationPermissions.FirstOrDefault(x => x.Permission == ApplicationPermission.Developer);

                // get items
                var hub = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Hub);
                var projectList = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Projects);
                var settingIndex = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Settings_Index);


                // get user permissions
                var hubPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == hub.ApplicationItemGuid);
                var projectListPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == projectList.ApplicationItemGuid);
                var settingIndexPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == settingIndex.ApplicationItemGuid);

                if (hubPermission.Permission.Hierarchy < notAllowed.Hierarchy)
                {
                    var main = new NavigationLink("Hub", 0, RegionNames.TopNavigationRegion, Module.TimeSplicer, NavigationMenu.Main);
                    model.Links.Add(main);
                }

                // need to change these
                if (projectListPermission.Permission.Hierarchy < notAllowed.Hierarchy)
                {
                    var projects = new NavigationLink("Projects", 1, RegionNames.TopNavigationRegion, Module.TimeSplicer, NavigationMenu.Projects);
                    model.Links.Add(projects);

                    var tasks = new NavigationLink("Tasks", 2, RegionNames.TopNavigationRegion, Module.TimeSplicer, NavigationMenu.Projects);
                    model.Links.Add(tasks);

                    var reports = new NavigationLink("Reports", 3, RegionNames.TopNavigationRegion, Module.Dashboard, NavigationMenu.Dashboard);
                    model.Links.Add(reports);
                }



                if (settingIndexPermission.Permission.Hierarchy == developer.Hierarchy)
                {
                    var settings = new NavigationLink("Settings", 4, RegionNames.TopNavigationRegion, Module.TimeSplicer, NavigationMenu.Settings);
                    model.Links.Add(settings);
                }



                // comment this out for now until i have it worked on 
                //var dash = new NavigationLink("Dashboard", 1, RegionNames.TopNavigationRegion, Module.TimeSplicer, NavigationMenu.Dashboard);
                //model.Links.Add(dash);

            }

            return model;
        }


        public TopNavigationViewModel Get(NavigationMenu menu)
        {
            throw new NotImplementedException();
        }
    }
}
